package com.aorise.service.general.impl;

import com.aorise.utils.GeneralEnum;
import com.aorise.mapper.general.GeneralMapper;
import com.aorise.model.general.StatisticsModel;
import com.aorise.model.request.ExceptionReq;
import com.aorise.service.general.GeneralService;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Auther: zhouhao
 * @Date: 2019/6/3
 * @Description:    运行一览图ServiceImpl
 */
@Service
public class GeneralServiceImpl implements GeneralService {

    @Autowired
    GeneralMapper generalMapper;

    @Override
    public List<Map<String,String>> getOnlineRate() {
        return generalMapper.getOnlineRate();
    }

    @Override
    public List<Map<String, String>> getRunGeneral() {
        return generalMapper.getRunGeneral();
    }

    @Override
    public void insertStatistics() {
        StatisticsModel statistics = getStatisticsModel();
        generalMapper.insertStatistics(statistics);
    }

    @Override
    public void updateStatistics() {
        StatisticsModel statistics = getStatisticsModel();
        generalMapper.updateStatistics(statistics);
    }

    @Override
    public Map getProGeneral() throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        StatisticsModel statis = generalMapper.getProGeneral();
        Map<String,String> map =new HashMap();
        map =BeanUtils.describe(statis);
//        异常总数
        double excCount = /*statis.getOffline() + */statis.getPic() + statis.getInten() + statis.getStriated() + statis.getSnow() + statis.getColor()
                + statis.getFrost() + statis.getScene() + statis.getBarrier() + statis.getSignal() + statis.getMirror() + statis.getMark();
        //格式化异常百分比
        double i = excCount / statis.getTotal();
        DecimalFormat df =new DecimalFormat("0.00");
        int format =(int)( Double.valueOf(df.format(i))*100);
        map.remove("class");
        map.remove("statisticalDate");
        map.remove("total");
        map.remove("online");
        map.remove("offline");
        map.remove("workTotal");
        map.remove("workFinish");
        map.remove("caService");

        List<ExceptionReq> list =map.entrySet().stream().sorted(Comparator.comparing(e->e.getValue()))
               .map(e -> new ExceptionReq( e.getKey(),Integer.valueOf(e.getValue()))).collect(Collectors.toList());
        Comparator<ExceptionReq> comparator = (e1, e2) -> e1.getExCount().compareTo(e2.getExCount());
        list.sort(comparator.reversed());
        List<ExceptionReq> lists = new ArrayList<>();
        if(list.size()>6){
            lists=list.subList(0,6);
            int asInt = list.subList(6, list.size()).stream().mapToInt(ExceptionReq::getExCount).sum();
            lists.add(ExceptionReq.builder().exName("other").exCount(asInt).build());
//            lists.add(ExceptionReq.builder().exName("offline").exCount(statis.getOffline()).build());
        }
        lists.forEach(l ->{
            setExName(l);
        });

        Map<String,Object> maps =new HashMap();
        maps.put("list",lists);
        //画面异常百分比
        maps.put("imagException",format+"");
        //异常总数
        maps.put("excCount",((int)excCount)+"");
        maps.put("statisticalDate",statis.getStatisticalDate());
        maps.put("total",statis.getTotal());

//        //画面异常百分比
//        map.put("imagException",format+"");
//        //异常总数
//        map.put("excCount",((int)excCount)+"");
        return maps;
    }

    private void setExName(ExceptionReq exc){
        for (GeneralEnum value:GeneralEnum.values()){
            if(exc.getExName().equals(value.getEn())){
                exc.setExName(value.getChinese());
            }
        }
    }

    @Override
    public Map getOperationSta(Date date) {
        return generalMapper.getOperationSta(date);
    }

    @Override
    public Map getAppoperations(String userID) {
        return generalMapper.getAppoperations(userID);
    }



    private StatisticsModel getStatisticsModel() {
        HashMap<String,Object> map=new HashMap<>();
        StatisticsModel statistics = generalMapper.getStatistics(map);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date d = new Date();
        statistics.setStatisticalDate(sdf.format(d));
        return statistics;
    }
}
